﻿using System.Web.Mvc;
using System.Linq;
using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
namespace ITXHWEB.Controllers
{
    public partial class ABFOLGEController
    {
        [ChildActionOnly]
        public ActionResult SSTEP(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            if (!MABF.IsSTEPReady)
            {
                INFO = "请扫描有效的步骤";
            }
            return PartialView(MABF);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SSTEP(ABFOLGEModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                STEPCHK(model);
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void STEPCHK(ABFOLGEModel model)
        {
            MABF.IsSTEPReady = false;
            if (model.FSTEP == null)
                return;
            if (model.FSTEP.Trim().Length < 3)
            {
                MABF.IsSTEPReady = false;
            }
            var result = ABFS.Where(p => p.FSTEP == model.FSTEP || p.ABFAGANG.Trim()==model.FSTEP.Trim() ).OrderBy(p => p.FSTEP);
            int remain = result.Count();
            foreach (var m in result)
            {
                m.WRUNID = WRUNID;
                m.IsABPReady = MABF.IsABPReady;
                m.IsPDCReady = MABF.IsPDCReady;
                m.IsStaffReady = MABF.IsStaffReady;
                m.PROG = MABF.PROG;
                m.NEWUSER = MABF.NEWUSER;  
                MABF = m;
                if (MABF.DTSTART.Trim().Length < 8)
                {
                    MABF.IsStart = false;
                }
                else
                {
                    MABF.IsStart = true;
                }
                if (MABF.MAM.MLFD=="0000")
                {
                    MABF.IsAMReady = false;
                }
                else
                {
                    MABF.IsAMReady = true;
                }

                MABF.IsSTEPReady = true;
                MABF.IsEnd = false;

                if (MABF.DTEND.Trim().Length == 8)
                {
                    MABF.IsEnd = true;
                    MABF.IsEndReady = true;
                    INFO = "步骤已经End/OK";
                    StartNewJob();
                }
            }
        }
    }
}
